package cn.chiship.framework.upms.biz.user.service.impl;

import cn.chiship.framework.upms.biz.user.entity.UpmsPermission;
import cn.chiship.framework.upms.biz.user.entity.UpmsUserPermission;
import cn.chiship.framework.upms.biz.user.entity.UpmsUserPermissionExample;
import cn.chiship.framework.upms.biz.user.mapper.UpmsPermissionApiMapper;
import cn.chiship.framework.upms.biz.user.mapper.UpmsPermissionMapper;
import cn.chiship.framework.upms.biz.user.mapper.UpmsUserPermissionMapper;
import cn.chiship.framework.upms.biz.user.service.UpmsUserPermissionService;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.core.id.SnowflakeIdUtil;
import cn.chiship.sdk.core.util.StringUtil;
import cn.chiship.sdk.framework.base.BaseServiceImpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * 用户权限关联表业务接口实现层
 * 2021/9/27
 *
 * @author lijian
 */
@Service
public class UpmsUserPermissionServiceImpl extends BaseServiceImpl<UpmsUserPermission, UpmsUserPermissionExample> implements UpmsUserPermissionService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UpmsUserPermissionServiceImpl.class);

    @Resource
    UpmsUserPermissionMapper upmsUserPermissionMapper;
    @Resource
    private UpmsPermissionMapper upmsPermissionMapper;
    @Resource
    private UpmsPermissionApiMapper upmsPermissionApiMapper;

    @Override
    public BaseResult getPermissionByUserId(String userId) {
        List<UpmsPermission> permissions = upmsPermissionApiMapper.selectPermissionByUserId(userId);
        return BaseResult.ok(permissions);
    }
    @Override
    public BaseResult userPermissionSave(String userId, List<String> permissionIdList) {
        UpmsUserPermissionExample upmsUserPermissionExample = new UpmsUserPermissionExample();
        UpmsUserPermissionExample.Criteria criteria = upmsUserPermissionExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<UpmsUserPermission> currentUserPermissions = upmsUserPermissionMapper.selectByExample(upmsUserPermissionExample);
        List<String> listPermissionId = new ArrayList<>();
        for (UpmsUserPermission userPermission : currentUserPermissions) {
            listPermissionId.add(userPermission.getPermissionId());
        }


        /**
         * 新增
         */
        List<String> notExists = new ArrayList<>(permissionIdList);
        notExists.removeAll(listPermissionId);
        if (!notExists.isEmpty()) {
            List<UpmsUserPermission> userPermissions = new ArrayList<>();
            for (String permissionId : notExists) {
                UpmsUserPermission userPermission = new UpmsUserPermission();
                userPermission.setId(SnowflakeIdUtil.generateStrId());
                userPermission.setIsDeleted(BaseConstants.NO);
                userPermission.setGmtCreated(System.currentTimeMillis());
                userPermission.setGmtModified(System.currentTimeMillis());
                userPermission.setUserId(userId);
                userPermission.setPermissionId(permissionId);
                userPermission.setType(Byte.valueOf("1"));
                userPermissions.add(userPermission);
            }
            upmsUserPermissionMapper.insertSelectiveBatch(userPermissions);
        }

        LOGGER.info("权限分配成功");
        return BaseResult.ok("权限分配成功");

    }

    @Override
    public BaseResult userPermissionRemove(String userId, List<String> permissionIdList) {
        UpmsUserPermissionExample upmsUserPermissionExample = new UpmsUserPermissionExample();
        UpmsUserPermissionExample.Criteria criteria = upmsUserPermissionExample.createCriteria();
        criteria.andUserIdEqualTo(userId);

        /**
         * 删除
         */
        List<String> listId = upmsPermissionMapper.getChildrenIdById(permissionIdList);
        listId.addAll(permissionIdList);
        criteria.andPermissionIdIn(listId);
        upmsUserPermissionMapper.deleteByExample(upmsUserPermissionExample);
        LOGGER.info("权限取消授权成功");
        return BaseResult.ok("权限取消授权成功");
    }

}
